package com.lgx.common.basic.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lgx.common.web.Pagination;

/**
 * 
 * @author acer
 *
 */
public class GeneralDao extends HibernateDaoSupport {

	/**
	 * 分页
	 * 
	 * @param sql
	 * @param params
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getList(String sql, Object[] params, int pageSize,int pageNum) {
		final String _sql = sql;
		final Object[] _params = params;
		final int _pageSize = pageSize;
		final int _pageNum = pageNum;
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(_sql);
				for (int i = 0; i < _params.length; i++) {
					query.setParameter(i, _params[i]);
				}
				List result = query.setFirstResult(_pageNum).setMaxResults(
						_pageSize).list();
				return result;
			}});
		return list;
	}
	
	/**
	 * 分页
	 * 
	 * @param sql
	 * @param params
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public Pagination getListPagination(String sql, Object[] params,int pageSize, int pageNum) {
		final String _sql = sql;
		final Object[] _params = params;
		final int _pageSize = pageSize;
		final int _pageNum = pageNum;
		String counthql = _sql.substring(_sql.toLowerCase().indexOf("from ")); 
        counthql = "select count(*) "+counthql; 
        final String queryStr = counthql;
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryStr);
				for (int i = 0; i < _params.length; i++) {
					query.setParameter(i, _params[i]);
				}
				List result = query.list();
				return result;
			}
		});
		int totalRecordNum = Integer.parseInt(list.get(0).toString());
		
		List data = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(_sql);
				for (int i = 0; i < _params.length; i++) {
					query.setParameter(i, _params[i]);
				}
				List result = query.setFirstResult((_pageNum-1)*_pageSize).setMaxResults(
						_pageSize).list();
				return result;
			}
		});
		
		int totalPageNum = totalRecordNum/pageSize;
		if(totalRecordNum%pageSize!=0) {
			totalPageNum+=1;
		}
		Pagination page = new Pagination();
		page.setPageNum(_pageNum);
		page.setPageSize(_pageSize);
		page.setTotal(totalRecordNum);
		page.setTotalPage(totalPageNum);
		page.setRows(data);
		return page;
	}
}
